﻿
Imports System.Runtime.CompilerServices
Imports osi.root.delegates
Imports osi.root.formation
Imports osi.root.procedure
Imports osi.root.utils

Public Module _allocator
    <Extension()> Public Function argument_allocator(Of T)(ByVal i As Func(Of var, pointer(Of T), event_comb)) _
                                                          As Func(Of String, pointer(Of T), event_comb)
        assert(Not i Is Nothing)
        Return Function(s As String, p As pointer(Of T)) As event_comb
                   Dim ec As event_comb = Nothing
                   Return New event_comb(Function() As Boolean
                                             Dim v As var = Nothing
                                             v = New var(s)
                                             ec = i(v, p)
                                             Return waitfor(ec) AndAlso
                                                    goto_next()
                                         End Function,
                                         Function() As Boolean
                                             Return ec.end_result() AndAlso
                                                    goto_end()
                                         End Function)
               End Function
    End Function

    <Extension()> Public Function argument_allocator(Of T)(ByVal i As _do_val_ref(Of var, T, Boolean)) _
                                                          As _do_val_ref(Of String, T, Boolean)
        assert(Not i Is Nothing)
        Return Function(s As String, ByRef o As T) As Boolean
                   Dim v As var = Nothing
                   v = New var(s)
                   Return i(v, o)
               End Function
    End Function

    <Extension()> Public Function argument_allocator(Of T)(ByVal i As Func(Of var, T)) _
                                                          As Func(Of String, T)
        assert(Not i Is Nothing)
        Return Function(s As String) As T
                   Dim v As var = Nothing
                   v = New var(s)
                   Return i(v)
               End Function
    End Function
End Module
